package com.xdtech.trans.service.impl;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xdtech.common.utils.DateUtil;
import com.xdtech.common.utils.DateUtil.DateStyle;
import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.trans.dao.TransAccountTradeDao;
import com.xdtech.trans.model.TransAccount;
import com.xdtech.trans.model.TransAccountTrade;
import com.xdtech.trans.service.TransAccountService;
import com.xdtech.trans.service.TransAccountTradeService;
import com.xdtech.trans.util.TransConstants;
import com.xdtech.trans.vo.TransAccountTradeItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-06-07 14:39:35
 * @since 1.0
 * @see
 */
@Service
public class TransAccountTradeServiceImpl implements TransAccountTradeService {
	private Log log = LogFactory.getLog(TransAccountTradeServiceImpl.class);
	@Autowired
	private TransAccountTradeDao transAccountTradeDao;
	@Autowired
	private BaseDao baseDao;
	@Autowired
	private TransAccountService transAccountService;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-07 14:39:35
	 * @modified by
	 * @param entity
	 */
	public void save(TransAccountTrade entity) {
		transAccountTradeDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-07 14:39:35
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<TransAccountTrade> entities) {
		transAccountTradeDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-07 14:39:35
	 * @modified by
	 * @param entity
	 */
	public void delete(TransAccountTrade entity) {
		transAccountTradeDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-07 14:39:35
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		transAccountTradeDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-07 14:39:35
	 * @modified by
	 * @param id
	 * @return
	 */
	public TransAccountTrade get(Long id) {
		return transAccountTradeDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-07 14:39:35
	 * @modified by
	 * @return
	 */
	public List<TransAccountTrade> getAll() {
		return transAccountTradeDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-07 14:39:35
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> transAccountTrades = null;
		long rows = 0;
		if (pg!=null) {
			Page<TransAccountTrade> page = transAccountTradeDao.findPage(pg,"from TransAccountTrade order by createTime desc", values);
			transAccountTrades = BeanUtils.copyListProperties(
					TransAccountTradeItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<TransAccountTrade> transAccountTradeList = transAccountTradeDao.find("from TransAccountTrade order by id desc", values);
			transAccountTrades = BeanUtils.copyListProperties(
					TransAccountTradeItem.class, transAccountTradeList);
			rows = transAccountTrades.size();
		}
		results.put("total", rows);
		results.put("rows", transAccountTrades);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-07 14:39:35
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateTransAccountTrade(TransAccountTradeItem item) {
		TransAccountTrade transAccountTrade = null;
		if (item.getId()==null) {
			transAccountTrade = new TransAccountTrade();
		}else {
			transAccountTrade = transAccountTradeDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(transAccountTrade, item);
		transAccountTradeDao.save(transAccountTrade);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-07 14:39:35
	 * @modified by
	 * @param transAccountTradeId
	 * @return
	 */
	public TransAccountTradeItem loadTransAccountTradeItem(Long transAccountTradeId) {
		TransAccountTrade transAccountTrade = transAccountTradeDao.get(transAccountTradeId);
		return TransAccountTradeItem.createItem(transAccountTrade);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-07 14:39:35
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteTransAccountTradeInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-07 14:39:35
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteTransAccountTradeInfo(List<Long> transAccountTradeIds) {
		for (Long id : transAccountTradeIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-07 14:39:35
	 * @modified by
	 * @return
	 */
	@Override
	public List<TransAccountTradeItem> loadItems() {
		List<TransAccountTrade> transAccountTrades = getAll();
		return TransAccountTradeItem.createItems(transAccountTrades);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-7下午6:50:14
	 * @modified by
	 * @param item
	 * @return
	 */
	@Override
	public ResultMessage inTransAccount(TransAccountTradeItem item) {
		ResultMessage rs = new ResultMessage();
		TransAccount transAccount = transAccountService.get(item.getAccountId());
		//验证金额
		double sum = transAccount.getAccountBalance()+item.getTradeBalance();
		DecimalFormat df = new DecimalFormat(".##");
		String st = df.format(sum);
		if (!st.equals(item.getAfterOperateBalance().toString())) {
			rs.setSuccess(false);
			rs.setMsg("交易金额统计结果不一致");
		}else {
			TransAccountTrade trade = new TransAccountTrade();
			BeanUtils.copyProperties(trade, item);
			trade.setTransAccount(transAccount);
			trade.setTradeType("in");
			trade.setTradeNo(DateUtil.getDate(new Date(), DateStyle.YYYYMMDDHHMMSSSSS));
			transAccountTradeDao.save(trade);
			//变更交易账户
			transAccount.setAccountBalance(Double.valueOf(st));
			transAccountService.save(transAccount);
		}
		return rs;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-7下午8:45:40
	 * @modified by
	 * @param item
	 * @return
	 */
	@Override
	public ResultMessage outTransAccount(TransAccountTradeItem item) {
		ResultMessage rs = new ResultMessage();
		TransAccount transAccount = transAccountService.get(item.getAccountId());
		//验证金额
		double sum = transAccount.getAccountBalance()-item.getTradeBalance();
		DecimalFormat df = new DecimalFormat(".##");
		String st = df.format(sum);
		if (!st.equals(item.getAfterOperateBalance().toString())) {
			rs.setSuccess(false);
			rs.setMsg("交易金额统计结果不一致");
		}else {
			TransAccountTrade trade = new TransAccountTrade();
			BeanUtils.copyProperties(trade, item);
			trade.setTransAccount(transAccount);
			//负数
			trade.setTradeBalance(-item.getTradeBalance());
			trade.setTradeType("out");
			trade.setTradeNo(DateUtil.getDate(new Date(), DateStyle.YYYYMMDDHHMMSSSSS));
			transAccountTradeDao.save(trade);
			//变更交易账户
			transAccount.setAccountBalance(Double.valueOf(st));
			transAccountService.save(transAccount);
		}
		return rs;
	}
	/* (非 Javadoc)
	 * createSendTrade
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param payTransAccount
	 * @param d
	 * @see com.xdtech.trans.service.TransAccountTradeService#createSendTrade(com.xdtech.trans.model.TransAccount, double)
	 */
	@Override
	public void createSendTradeRecord(TransAccount payTransAccount, double d,String operator,String tradeOrderNo)
	{
		DecimalFormat df = new DecimalFormat(".##");
		String st = df.format(d);
		TransAccountTrade trade = new TransAccountTrade();
		trade.setTransAccount(payTransAccount);
		trade.setTradeType("send");
		trade.setTradeNo(DateUtil.getDate(new Date(), DateStyle.YYYYMMDDHHMMSSSSS));
		trade.setOperator(operator);
		trade.setTradeOrderNo(tradeOrderNo);
		trade.setTradeBalance(Double.valueOf(st));
		trade.setTradeDate(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
		transAccountTradeDao.save(trade);
		//变更交易账户
		payTransAccount.setAccountBalance(payTransAccount.getAccountBalance()+d);
		transAccountService.save(payTransAccount);
	}
	
	/* (非 Javadoc)
	 * createTransTradeRecord
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param transAccount
	 * @param money
	 * @param string
	 * @param operator
	 * @see com.xdtech.trans.service.TransAccountTradeService#createTransTradeRecord(com.xdtech.trans.model.TransAccount, double, java.lang.String, java.lang.String)
	 */
	@Override
	public void createTransTradeRecord(TransAccount transAccount, double money, String type, String operator,String tradeOrderNo,boolean isCalculate)
	{
		DecimalFormat df = new DecimalFormat(".##");
		String st = df.format(money);
		TransAccountTrade trade = new TransAccountTrade();
		trade.setTransAccount(transAccount);
		trade.setTradeType(type);
		trade.setTradeNo(DateUtil.getDate(new Date(), DateStyle.YYYYMMDDHHMMSSSSS));
		trade.setOperator(operator);
		trade.setTradeOrderNo(tradeOrderNo);
		trade.setTradeBalance(Double.valueOf(st));
		trade.setTradeDate(DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
		trade.setIsCash(!isCalculate+"");
		transAccountTradeDao.save(trade);
		if (isCalculate) {
			//是否进行资金账户交易统计
			String accountBlance = df.format(transAccount.getAccountBalance()+money);
			//变更交易账户
			transAccount.setAccountBalance(Double.valueOf(accountBlance));
			transAccountService.save(transAccount);
		}
		
	}

}
